package com.example.swimmingpool.service.vip;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import com.example.swimmingpool.SwimmingPoolThreadLocal;
import com.example.swimmingpool.entity.vip.Vip;
import jakarta.annotation.Resource;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

@Service
public class VipService {
    @Resource
    private MongoTemplate mongoTemplate;

    public boolean isVip() {
        var userId = SwimmingPoolThreadLocal.getUserId();
        return mongoTemplate.exists(Query.query(Criteria.where("userId").is(userId)), Vip.class);
    }

    public boolean becomeVip() {
        var userId = SwimmingPoolThreadLocal.getUserId();
        var now = LocalDateTimeUtil.format(LocalDateTimeUtil.now(), DatePattern.NORM_DATETIME_PATTERN);
        Vip vip = new Vip()
                .setCreateTime(now)
                .setUserId(String.valueOf(userId));
        var res = mongoTemplate.insert(vip);
        return StrUtil.isNotBlank(res.getId());

    }
}
